package com.sddr.base.frame.observe.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.sddr.base.frame.metadata.MetaDataOperation;
import com.sddr.base.frame.observe.ConfigRuleObserve;
import com.sddr.common.entity.ConfigRule;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @className DefaultConfigRuleObserveImpl.java 被通知的观察者默认实现类
 * @author 山顶洞人郑某
 * @date 2025 07 13
 */
@AllArgsConstructor
@Slf4j
public class DefaultConfigRuleObserveImpl implements ConfigRuleObserve {

    /**
     * 元数据操作接口
     */
    private final MetaDataOperation metaDataOperation;
    /**
     * 通知规则变更观察者
     *
     * @param content
     */
    @Override
    public void notifyCinfigRule(final String content) {
        if (!StrUtil.isEmpty(content)) {
            log.info("收到配置变更通知,{}", content);
            try {
                metaDataOperation.setRule(JSONObject.parseObject(content, ConfigRule.class));
            } catch (Exception e) {
                log.error("解析配置失败,{}", e);
            }
        }
    }
}